/*
 * @Author: xuhongling
 * @Date:   2023-08-24 14:51:00
 * @Last Modified by:   xuhongling
 * @Last Modified time: 2024-10-14 16:52:02
 */
import dateTimeLabelFormat from '@/hooks/web/useDateTimeFormat';
import { Hformat } from '@/hooks/web/useHQPFormat';

// ECharts Option
const setChartOption = (stationData, chartData) => {
  // 格式化数组水位
  const HArrayFormat = (data) => {
    if (data.length === 0) return [];
    const arrData: any[] = [];
    for (let i = 0; i < data.length; i++) {
      arrData.push([data[i][0], Hformat(data[i][1])]);
    }
    return arrData;
  };

  const chartDataQ: any[] = [];
  for (let i = 0; i < chartData.length; i++) {
    chartDataQ.push([chartData[i].tmMin, chartData[i].q]);
  }
  chartDataQ.reverse();
  // 设置 ECharts Option
  const chartOption = {
    color: ['#000', '#ff0000', '#0000ff', '#5470c6', '#fc8452', '#9a60b4', '#ee6666', '#73c0de', '#3ba272'],
    title: {
      text: `${stationData.stnm}流量过程线`,
      left: 'center',
      top: '10',
      textStyle: {
        fontSize: 16,
      },
    },
    legend: {
      type: 'scroll',
      left: 'center',
      top: '44',
      itemWidth: 16,
      itemHeight: 10,
      data: ['流量'],
    },
    xAxis: {
      type: 'time',
      boundaryGap: false,
      axisLine: {
        lineStyle: {
          type: 'solid',
          color: '#333',
        },
      },
      axisTick: {
        show: true,
        inside: true,
        interval: 'auto',
        lineStyle: {
          color: '#999',
        },
      },
      splitLine: {
        show: false,
        lineStyle: {
          type: 'dashed',
          color: '#f4f4f4',
        },
      },
      axisLabel: {
        show: true,
        margin: 14,
        color: '#333',
        fontSize: 12,
        formatter: (value, index) => {
          return dateTimeLabelFormat(value, index);
        },
      },
    },
    yAxis: [
      {
        type: 'value',
        position: 'left',
        name: '流量(m³/s)',
        scale: true,
        splitNumber: 5,
        boundaryGap: ['0%', '5%'],
        nameTextStyle: {
          color: '#333',
        },
        axisTick: {
          show: false,
          inside: true,
          lineStyle: {
            color: '#999',
          },
        },
        axisLine: {
          show: true,
          lineStyle: {
            color: '#333',
          },
        },
        axisLabel: {
          show: true,
          color: '#333',
          fontSize: 12,
          formatter(value) {
            if (value) {
              return Hformat(value);
            }
          },
        },
        splitLine: {
          show: false,
          lineStyle: {
            type: 'dashed',
            color: '#f4f4f4',
          },
        },
      },
    ],
    series: [
      {
        name: '流量',
        type: 'line',
        yAxisIndex: 0,
        showSymbol: false,
        connectNulls: true,
        lineStyle: {
          color: '#000',
          width: 2,
        },
        markPoint: {
          symbol: 'pin',
          symbolSize: [90, 40],
          symbolRotate: (params) => {
            if (params.name === '最大值') {
              return 0;
            }
            return 180;
          },
          data: [
            {
              type: 'max',
              name: '最大值',
              label: {
                formatter: (params) => {
                  return Hformat(params.value);
                },
              },
              itemStyle: {
                color: 'rgba(255, 0, 0, 0.8)',
              },
            },
            {
              type: 'min',
              name: '最小值',
              label: {
                padding: [16, 0, 0, 0],
                formatter: (params) => {
                  return Hformat(params.value);
                },
              },
              itemStyle: {
                color: 'rgba(10, 210, 0, 0.8)',
              },
            },
          ],
        },
        data: HArrayFormat(chartDataQ),
      },
    ],
  };
  return chartOption;
};

export default setChartOption;
